<!-- 这个组件没用到 -->
<template lang="pug">
section
  FixedScroll(:minusBottomHeight="minusBottomHeight")
    div(slot="fixed")
      PageHeader(:pageTitle="pageTitle")

      ScanSearch(
          placeholder="序列号"
          @search="search"
          @scan="search"
          )

    ColoredTable.table(
        slot="scroll"
        :headList="headList"
        :body="barcodeBody"
        :statusMap="statusMap"
        :isLoading="isLoading"
        )

</template>

<script>
import api from "$api"
import util from "$util"

const STATUS_OK = ""
const STATUS_ERROR = "error"

export default {
  name: "BarcodeInfo",
  props: {
    forceUpdate: {
      type: Boolean,
      default: false,
    },
    kind: {
      type: String,
      default: "", // refund
    },
    scanPageBody: {
      type: Object,
      default: () => {},
    },
    barcodeBody: {
      type: Object,
      default: () => {},
    },
    statusMap: {
      type: Object,
      default: () => {},
    },
    minusBottomHeight: {
      type: Number,
      default: 0,
    },
    pageTitle: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      isLoading: false,

      headList: [
        { field: "BarcodeData", title: "序列号", checked: true },
        { field: "Qty", title: "数量", checked: true },
        { field: "PartNo", title: "物料号", checked: true },
        { field: "PartName", title: "物料名称", checked: true },
      ],
      codeMap: {},
    }
  },
  watch: {
    forceUpdate(v) {
      if (v) {
        this.registerScanner()
        this.$forceUpdate()
      }
    }
  },
  computed: {
    scanMap() {
      let m = {}
      let i = 0

      switch (this.kind) {
        case "refund":
          for (let item of this.scanPageBody.list) {
            m[`${item.PartNo}-${item.BatchNo}`] = {
              index: i,
              IsBatch: item.IsBatch,
            }
            i++
          }
          break
        default:
          for (let item of this.scanPageBody.list) {
            m[item.PartNo] = {
              index: i,
              IsBatch: item.IsBatch,
            }
            i++
          }
          break
      }

      return m
    },
  },
  methods: {
    async search(value) {
      if (value === "") {
        return
      }

      if (this.codeMap[value]) {
        return
      }

      try {
        let data = await api.StockMoveGetInfo({
          BarcodeData: value,
        })

        let v, index, IsBatch

        switch (this.kind) {
          case "refund":
            v = this.scanMap[`${data.PartNo}-${data.BatchNo}`]
            break
          default:
            v = this.scanMap[data.PartNo]
            break
        }

        if (v) {
          index = v.index
          IsBatch = v.IsBatch
        }
        if (IsBatch === 1) {
          util.Alert(`物料${data.PartNo}维护的管理模式错误，请维护库位物料关系数据后操作`, "错误")
          this.codeMap = {}
          this.$emit("reset-all")
          return
        }

        let status = ""
        if (index != undefined) {
          this.$emit("add-num", index)
          status = STATUS_OK
        } else {
          status = STATUS_ERROR
        }

        this.$emit("add-barcode", data, status)
        this.codeMap[value] = true

      } catch(err) {
        this.$root.error(err)
      } finally {
        this.isLoading = false
      }
    },

    registerScanner() {
      if (window.plugins) {
        plugins.honeywell.listenForScans(
          (data) => {
            this.search(data)
          },
          (err) => {
            // util.Toast("scan error: " + err)
          },
        )
      }
    },
  },
}
</script>

<style lang="stylus" scoped>

.table
  scan-table()

</style>
